package meatslope2.application;

import javax.microedition.khronos.opengles.GL10;

import meatslope2.constants.TileConstants;
import meatslope2.gameengine.engine.EngineRenderer;
import meatslope2.gameengine.engine.IRendererListener;
import meatslope2.gameengine.input.InputSystem;
import meatslope2.gameengine.scene.Scene;
import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;

public class GameApplication extends Activity implements IRendererListener
{
	private Game game;
	private InputSystem inputSystem;
	
	private EngineRenderer engineRenderer;
	private GLSurfaceView surfaceView;
	
	private Scene scene;
	
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        
        //Create scene
        this.scene = Scene.Factory.generateSampleLevel1(this);
        
        //Pass scene into new engine
		this.game = new Game(this.scene);
		this.engineRenderer = new EngineRenderer(this, game);
		
		//Create input system with game
        this.inputSystem = new InputSystem(this.game);
		
		//Create surface to draw on, set it's renderer to the one we just created
		this.surfaceView = new GLSurfaceView(this);
		this.surfaceView.setRenderer(engineRenderer);
        
        this.setContentView(surfaceView);
    }
	
	@Override
	public synchronized void onSurfaceCreated(GL10 pGL) 
	{
		//We can load bitmaps in the scene
		this.scene.loadBitmaps(pGL);
	
		//initialize all of our static classes
		initializeLibrary(pGL);
		
		//Now start the update cycle
		this.game.start();
	}

    private synchronized void initializeLibrary(GL10 pGL) 
    {
		TileConstants.loadBitmaps(this, pGL);
		new SystemRegistry(this.scene);
	}

	@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    {
    	return inputSystem.onKeyDown(keyCode, event);
    }
     
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) 
    {
    	return inputSystem.onKeyUp(keyCode, event);
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) 
    {
    	return inputSystem.onTouchEvent(event);
    }
}
